package com.xywang.servlet.provider;

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import com.xywang.pojo.Bill;
import com.xywang.pojo.Provider;
import com.xywang.pojo.User;
import com.xywang.service.bill.BillService;
import com.xywang.service.bill.BillServiceImpl;
import com.xywang.service.provider.ProviderService;
import com.xywang.service.provider.ProviderServiceImpl;
import com.xywang.util.Constants;
import com.xywang.util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ServletProvider extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method != null && method.equals("query")) {
            this.query(req, resp);
        } else if(method != null && method.equals("add")){
            this.add(req, resp);
        }else if (method != null && method.equals("delprovider")) {
            this.delprovider(req, resp);
        }else if (method != null && method.equals("modify")) {
            this.getProviderByIdModify(req, resp);
        }else if (method != null && method.equals("modifysave")) {
            this.modify(req, resp);
        }else if (method != null && method.equals("view")) {
            this.getProviderByIdView(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    // 展示供应商
    public void query(HttpServletRequest req, HttpServletResponse resp){
        // 创建服务层对象，之后调用他们的方法
        ProviderService providerService = new ProviderServiceImpl();
        // 获取前端数据
        String queryProCode = req.getParameter("queryProCode");
        String queryProName = req.getParameter("queryProName");

        // System.out.println("ServletProvider--query--queryProCode:"+queryProCode);
        // System.out.println("ServletProvider--query--queryProName:"+queryProName);
        if (StringUtils.isNullOrEmpty(queryProName)) {
            queryProName = "";
        }
        if (StringUtils.isNullOrEmpty(queryProCode)) {
            queryProCode = "";
        }

        // 获取供应商列表，输出到前端
        List<Provider> providerList = providerService.getProviderListService(queryProCode, queryProName);

        // 像前端发送数据

        req.setAttribute("providerList", providerList);
        req.setAttribute("queryProCode", queryProCode);
        req.setAttribute("queryProName", queryProName);
        try {
            req.getRequestDispatcher("providerlist.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }
    // 添加供应商
    public void add(HttpServletRequest req, HttpServletResponse resp){
        // 获取前端参数
        String proCode = req.getParameter("proCode");// 供应商编码
        String proName = req.getParameter("proName");// 供应商名称
        String proContact = req.getParameter("proContact");// 联系人
        String proPhone = req.getParameter("proPhone");// 联系电话
        String proAddress = req.getParameter("proAddress");// 联系地址
        String proFax = req.getParameter("proFax");// 传真
        String proDesc = req.getParameter("proDesc");// 描述
        // 将前端参数封装到provider对象
        Provider provider = new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProPhone(proPhone);
        provider.setProContact(proContact);
        provider.setProAddress(proAddress);
        provider.setProFax(proFax);
        provider.setProDesc(proDesc);
        provider.setCreatedBy(((User) req.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setCreationDate(new Date());
        // 调用底层方法实现插入，返回boolean值
        boolean flag;
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        flag = providerService.addProService(provider);
        if (flag) {
            try {
                resp.sendRedirect(req.getContextPath() + "/jsp/provider.do?method=query");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                req.getRequestDispatcher("provideradd.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 删除供应商
    public void delprovider(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("proid");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if (!StringUtils.isNullOrEmpty(id)) {
            ProviderService providerService = new ProviderServiceImpl();
            // 如果有订单的供应商是供应商id号为proid的，则该供应商不能删除
            int billCount = providerService.deleteProviderByIdService(id);
            if (billCount == 0) {//删除成功
                resultMap.put("delResult", "true");
            } else if (billCount == -1) {//删除失败
                resultMap.put("delResult", "false");
            } else if (billCount > 0) {//该供应商下有订单，不能删除，返回订单数
                resultMap.put("delResult", String.valueOf(billCount));
            }
        } else {
            resultMap.put("delResult", "notexit");
        }

        //把resultMap转换成json对象输出
        PrintWriter outPrintWriter = null;
        try {
            resp.setContentType("application/json");
            outPrintWriter = resp.getWriter();
            outPrintWriter.write(JSONArray.toJSONString(resultMap));
            outPrintWriter.flush();
            outPrintWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    // 获取供应商修改
    public void getProviderByIdModify(HttpServletRequest req, HttpServletResponse resp){
        String proid = req.getParameter("proid");
        if (!StringUtils.isNullOrEmpty(proid)) {
            //调用后台方法得到provider对象
            ProviderServiceImpl providerService = new ProviderServiceImpl();
            Provider provider = providerService.getProviderByIdService(proid);
            req.setAttribute("provider", provider);
            try {
                req.getRequestDispatcher("providermodify.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 获取供应商展示
    public void getProviderByIdView(HttpServletRequest req, HttpServletResponse resp){
        String proid = req.getParameter("proid");
        if (!StringUtils.isNullOrEmpty(proid)) {
            //调用后台方法得到provider对象
            ProviderServiceImpl providerService = new ProviderServiceImpl();
            Provider provider = providerService.getProviderByIdService(proid);
            req.setAttribute("provider", provider);
            try {
                req.getRequestDispatcher("providerview.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 修改供应商
    public void modify(HttpServletRequest req, HttpServletResponse resp){
        String proCode = req.getParameter("proCode");//供应商编码
        String proName = req.getParameter("proName");//供应商名称
        String proContact = req.getParameter("proContact");//联系人
        String proPhone = req.getParameter("proPhone");//联系电话
        String proAddress = req.getParameter("proAddress");//联系地址
        String proFax = req.getParameter("proFax");//传真
        String proDesc = req.getParameter("proDesc");//描述
        String proid = req.getParameter("proid");//

        Provider provider = new Provider();
        provider.setId(Integer.valueOf(proid));
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProFax(proFax);
        provider.setProAddress(proAddress);
        provider.setProDesc(proDesc);
        provider.setModifyBy(((User) req.getSession().getAttribute(Constants.USER_SESSION)).getId());
        provider.setModifyDate(new Date());

        ProviderService providerService = new ProviderServiceImpl();
        boolean flag = providerService.modifyService(provider);
        if (flag) {
            try {
                resp.sendRedirect(req.getContextPath() + "/jsp/provider.do?method=query");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                req.getRequestDispatcher("providermodify.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
    }

}
